package com.woniuxy.commons.util;

import cn.hutool.json.JSONObject;
import com.nimbusds.jose.JWSObject;

import java.text.ParseException;
import java.util.List;

public class GetTokenUserLogin {

    public static LoginUserDto getTokenUserLogin(){

        LoginUserDto loginUserDto = new LoginUserDto();
        String token = WebUtil.getRequest().getHeader("Authorization");
        String realtoken = token.replace("Bearer " , "");
        JWSObject jwsObject = null;
        try {
            jwsObject = JWSObject.parse(realtoken);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String userStr = jwsObject.getPayload().toString();
        JSONObject userJsonObject = new JSONObject(userStr);
        System.out.println(userStr);
        String loginName =(String)userJsonObject.get("LoginName");
        Integer LoginId =(Integer)userJsonObject.get("LoginId");
        List<String> LoginRole =(List<String>)userJsonObject.get("LoginRole");
        loginUserDto.setLoginId(LoginId);
        loginUserDto.setLoginRole(Integer.parseInt(LoginRole.get(0)));
        loginUserDto.setLoginName(loginName);
        return  loginUserDto;
    }
}
